package com.shalanzi.hospital.controller;

import com.shalanzi.hospital.base.BasePath;
import com.shalanzi.hospital.pojo.User;
import com.shalanzi.hospital.query.UserQuery;
import com.shalanzi.hospital.service.ClassService;
import com.shalanzi.hospital.service.UserService;
import com.shalanzi.hospital.vo.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Dream_飞翔
 * @date 2021/9/29
 * @time 21:58
 * @email 1072876976@qq.com
 */
@Controller
@RequestMapping("/class")
public class ClassController extends BasePath {
    @Resource
    private UserService userService;
    @Resource
    private ClassService classService;

    @RequestMapping("/index")
    public String index() {
        return "class/index";
    }

    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(Integer userId, HttpServletRequest request) {
        if (userId != null) {
            User classUser = userService.selectClassUserById(userId);
            request.getSession().setAttribute("classUser", classUser);
        }
        return "class/update";
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> getClassList(UserQuery userQuery) {
        return classService.getClassList(userQuery);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateClassUser(User user) {
        Integer row = userService.updateUserState(user);
        return row > 0 ? ResultInfo.success("排班状态更新成功！") : ResultInfo.fail("排班状态更新失败！");
    }

    @GetMapping("/queryAllStates")
    @ResponseBody
    public List<Map<String, Object>> queryAllStates() {
        return userService.queryAllStates();
    }
}
